package org.itboys.mobile.service.mysql.web;


import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_service_web
 * 当前为单规格。如果需要多规格，请自行扩展
 */
@Service
public class SundayProductService extends BaseMySqlService<SundayProduct> {

	@Autowired
	private SundayUploadService uploadService;
	/**
	 * 分页查询商品
	 *
	 * @param request
	 * @return
	 */
	public PageResult<SundayProduct> selectProduct(HttpServletRequest request, List<Long> ids) {
		Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "name", "number", "categoryId", "categoryName","type");
		if (ListUtil.isNotNull(ids)) {
			param.put("ids", ids);
		}
		List<SundayProduct> productList = super.select(param);
		return new PageResult<SundayProduct>(super.select(param), super.count(param));
	}

	/**
	 * 新增/修改商品
	 * @param product
	 * @param imageFile
	 * @param rollImageFiles

	 * @throws IOException
	 */
	@Transactional
	public void save(
			SundayProduct product,
			MultipartFile imageFile,
			MultipartFile[] rollImageFiles
	) throws  Exception {
		//保存主图
		Map<String, String> map = uploadService.uploadSingle(imageFile);
		if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){

			product.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
		}

		//编辑
		if(LongUtil.isNotZreo(product.getId())){
			//product.setUpdater(creator);
			//校验编号
			if(StringUtils.isNotEmpty(product.getNumber())){
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("id",product.getId());
				param.put("number_equal", product.getNumber());
				//param.put("type", Finals.TYPE_TIANJIE);
				//param.put("isDeletedNotEqual", Finals.YES);
				if(ListUtil.isNotNull(super.select(param))){
					throw new ErrorException("商品编号不能重复");
				}
			}
			super.update(product);
		}else{
			//校验编号
			Map<String, Object> param = new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(product.getNumber())){

				param.put("number_equal", product.getNumber());

				//param.put("isDeletedNotEqual", Finals.YES);
				if(ListUtil.isNotNull(super.select(param))){
					throw new ErrorException("商品编号不能重复");
				}
			}
			//product.setIsDeleted(Finals.NO);
			//product.setType(Finals.TYPE_TIANJIE);
			//product.setZoneId(zoneId);
			super.insert(product);
		}
		//保存轮播图
		List<Map<String, String>> maps = uploadService.
				uploadMultiWithRecord(rollImageFiles,product.getId(),SundayProduct.class.getSimpleName(),"商品ID"+product.getId()+",的轮播图");

	}
	/**
	 * 查询商品详情。后台
	 * @param productId
	 * @return
	 */
	public SundayProduct findOne(Long productId){
		SundayProduct product=null;
		if(LongUtil.isNotZreo(productId)){
			product = super.find(productId);
			//TianJieProduct
			//轮播图
			Map<String,Object> imgParam = new HashMap<String, Object>();
			imgParam.put("outClassName", product.getClass().getSimpleName().toString());
			imgParam.put("outClassId", productId);
			List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
			product.setRollImages(rollImages);

		}else{
			product=new SundayProduct();
			product.setId(0l);
			product.setType(SundayCommonConstants.type_mall);
		}

		return product;
	}
	//设置推荐。

	/**
	 * 设置为推荐
	 * @param productId
	 * @param status
	 * @param type 1，首页推荐。2其他待定
	 */
	public void setRecommend(Long productId,Integer status,Integer type){
			SundayProduct product = new SundayProduct();
			product.setId(productId);
			if(type==1){
				product.setIsIndexRecommend(status);
			}
			super.update(product);

	}

	/**
	 * 上下架
	 * @param id
	 * @param status
	 */
	public void change(Long id,Integer status){
		SundayProduct product = new SundayProduct();
		product.setId(id);
		product.setStatus(status);
		super.update(product);

	}
}

